var mqtt = require('../utils/mqtt.js');
//一个全局变量...
var client = null;
let searchDisabled=true;
const app=getApp();
Component({
	data: {
		selected : 0,
    color: "#7A7E83",
    selectedColor: "#0F9FD9",
		list: [
			{
				icon: 'icon-shouye',
				text: '校园',
				pagePath: '/pages/campus/campus'
			},
			{
				icon: 'icon-jingwuicon_svg-',
				text: '树洞',
				pagePath: '/pages/student/student'
			},
			{
				icon: 'icon-bangdan',
				text: '搜索',
				pagePath: '/pages/search/search'
			},
			{
				icon: 'icon-31wode',
				text: '我的',
				pagePath: '/pages/profile/profile'
			}
		],
		unreadNum:0
	},
	attached() {
		console.log('tabbar被挂载了')
		this.setData({selected:wx.getStorageSync('selected')})
	},
	methods: {
		// 发帖
		async onClickPublish() {
			console.log("onClickPublish")
			let name = app.globalData.userInfo.name,userInfo = app.globalData.userInfo
			if (!name) {
				wx.showToast({
					title: "请先去[我的->个人信息]界面完成身份验证~",
					icon: 'none'
				})
				searchDisabled = false
			} else if (!(userInfo.ifValid == 'Y')) {
				wx.showToast({
					title: "请等待身份验证通过~",
					icon: 'none'
				})
				searchDisabled = false
			} else {
				let index=wx.getStorageSync('selected')
				//if(index != 0 && index != 1) index = 1
				if(index == 1){
					wx.navigateTo({
						url: `../campus-edit/campus-edit?type=5&schoolId=`+wx.getStorageSync('schoolId'),
						success: function(res) {
							// 通过eventChannel向被打开页面传送数据
							res.eventChannel.emit('acceptDataFromOpenerPage', { data: userInfo })
						}
					})
				}else{
					wx.navigateTo({
						url: `../campus-edit/campus-edit`,
						success: function(res) {
							// 通过eventChannel向被打开页面传送数据
							res.eventChannel.emit('acceptDataFromOpenerPage', { data: userInfo })
						}
					})
				} 
			}
		},
		init(){
			this.setData({selected:wx.getStorageSync('selected')})
		},
		switchTab(e) {
      const data = e.currentTarget.dataset
			const url = data.path,that=this
			that.setData({selected:data.index})
			wx.setStorageSync('selected', data.index)
			console.log(that.data.selected)
			wx.switchTab({url:url})
    },

		// onChange(event) {
		// 	this.setData({ active: event.detail })
		// 	wx.switchTab({
		// 		url: this.data.list[event.detail].url
		// 	});
		// },
		// setUnreadNum(unreadNum,menu){
		// 	//console.log(unreadNum)
		// 	//console.log(menu)
		// 	menu.setData({unreadNum:unreadNum})
		// },
		// init() {
		// 	let _url = req.actions.ym+"invitation/getUnreadCommet",that=this,unreadnum = app.globalData.unreadNum
		// 	this.setData({unreadNum:unreadnum && unreadnum > 0?getApp().globalData.unreadNum:''})
		// 	//console.log("initinitinitinit")
		// 	const pages = getCurrentPages();
 		// 	const currentPage = pages[pages.length - 1];
		// 	//console.log(currentPage.route)
		// 	let interval = setInterval(function(){
		// 		let userInfo = app.globalData.userInfo
		// 			//console.log(userInfo)
		// 			//console.log(that.getTabBar())
		// 			let flag = (currentPage.route == 'pages/campus/campus' && that.data.active == 0) || (currentPage.route == 'pages/student/student' && that.data.active == 1)
		// 			|| (currentPage.route == 'pages/profile/profile' && that.data.active == 2)
		// 			if(userInfo && flag){
		// 				utils.myResquest({openid:userInfo.openid},_url,function(res){
		// 					//console.log(res.data.data.length)
		// 					app.globalData.unreadNum = res.data.data.length
		// 					that.setData({unreadNum:res.data.data.length})
		// 					if(app.profileUnReadCallBack){
		// 						app.profileUnReadCallBack(res.data.data.length,app.profile)
		// 					}
		// 				})
		// 			}else{
		// 				clearInterval(interval)
		// 			}
		// 		},2000)
		// 		this.setData({interval:interval})
		// 		//getApp().setCallBack("menuUnRead",this.setUnreadNum,this)
		// 		const page = getCurrentPages().pop();
		// 		this.setData({
		// 			active: this.data.list.findIndex(item => item.url === `/${page.route}`)
		// 		})
		// }
	}
});
